﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Compression;
using System.Net;

namespace scfamanager
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            download("Maps",1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            download("Maps",2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            download("Maps",3);
        }

        private void download(string folder, int mapid)
        {
            string url = "http://dropcanvas.com/59x3t/" + mapid.ToString();
            string tempPath = System.IO.Path.GetTempFileName();
            string targetPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\My Games\Gas Powered Games\Supreme Commander Forged Alliance\" + folder;

            try
            {
                WebClient client = new WebClient();
                client.DownloadFile(url, tempPath);
                ZipFile.ExtractToDirectory(tempPath, targetPath);
                System.IO.File.Delete(tempPath);
                MessageBox.Show("Install complete!");
            }
            catch (Exception ex)
            {
                while (ex != null)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ex = ex.InnerException;
                }
            }
        }
        private void sorianInstall()
        {
            string url = "http://dropcanvas.com/59x3t/5";
            string tempPath = System.IO.Path.GetTempFileName();
            string targetPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\Steam\steamapps\common\Supreme Commander Forged Alliance\gamedata\";

            try
            {
                WebClient client = new WebClient();
                client.DownloadFile(url, tempPath);
                ZipFile.ExtractToDirectory(tempPath, targetPath);
                System.IO.File.Delete(tempPath);
                MessageBox.Show("Install complete!");
            }
            catch (Exception ex)
            {
                while (ex != null)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ex = ex.InnerException;
                }
            }
        }
        private void sorianRemove()
        {
            string gamedataPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\Steam\steamapps\common\Supreme Commander Forged Alliance\gamedata\";
            string[] sorianFiles = { 
                                       "02_sorian_ai_pack.scd",
                                       "03_lobbyenhancement.scd",
                                       "lemreadme.txt",
                                       "readme.txt"
                                   };
            try
            {
                foreach (string sorianFileName in sorianFiles)
                {
                    System.IO.File.Delete(gamedataPath + sorianFileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Removing files failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            download("Mods", 4);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            sorianInstall();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            sorianRemove();
        }
    }
}
